﻿using System.Windows;
using System.Windows.Controls;
using VM.Common.Enums;
using VM.Dialogs.Views;
using VM.Start.Core;
using VM.Start.Dialogs.Views;

namespace Plugin.BeadGuide
{
    /// <summary>
    /// BeadGuideView.xaml 的交互逻辑
    /// </summary>
    public partial class BeadGuideView : ModuleViewBase
    {
        public BeadGuideView()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MoveUp_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null && button.DataContext is TemplateModel model)
            {
                var viewModel = DataContext as BeadGuideViewModel;
                if (viewModel != null)
                {
                    int currentIndex = model.Index;

                    // Check if the model can be moved up
                    if (currentIndex > 0)
                    {
                        // Swap the current model with the one above it
                        var itemAbove = viewModel.TemplateList[currentIndex - 1];
                        viewModel.TemplateList[currentIndex] = itemAbove;
                        viewModel.TemplateList[currentIndex - 1] = model;

                        // Update indices
                        model.Index = currentIndex - 1;
                        itemAbove.Index = currentIndex;

                        // Update all indices
                        UpdateTemplateIndices(viewModel);
                    }
                }
            }
        }

        private void MoveDown_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null && button.DataContext is TemplateModel model)
            {
                var viewModel = DataContext as BeadGuideViewModel;
                if (viewModel != null)
                {
                    int currentIndex = model.Index;

                    // Check if the model can be moved down
                    if (currentIndex < viewModel.TemplateList.Count - 1)
                    {
                        // Swap the current model with the one below it
                        var itemBelow = viewModel.TemplateList[currentIndex + 1];
                        viewModel.TemplateList[currentIndex] = itemBelow;
                        viewModel.TemplateList[currentIndex + 1] = model;

                        // Update indices
                        model.Index = currentIndex + 1;
                        itemBelow.Index = currentIndex;

                        // Update all indices
                        UpdateTemplateIndices(viewModel);
                    }
                }
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.DataContext is TemplateModel model)
            {
                if (DataContext is BeadGuideViewModel viewModel)
                {
                    // 找到要删除的模板的索引
                    int modelIndex = viewModel.TemplateList.IndexOf(model);
                    if (modelIndex >= 0)
                    {
                        // 确定新的选中模板
                        if (model == viewModel.SelectedTemplate)
                        {
                            if (modelIndex > 0) // 优先选择上一个模板
                            {
                                viewModel.SelectedTemplate = viewModel.TemplateList[modelIndex - 1];
                            }
                            else if (modelIndex < viewModel.TemplateList.Count - 1) // 如果没有上一个模板，则选择下一个模板
                            {
                                viewModel.SelectedTemplate = viewModel.TemplateList[modelIndex + 1];
                            }
                            else // 如果是最后一个模板
                            {
                                viewModel.SelectedTemplate = null;
                            }
                        }

                        // 从列表中移除模板
                        viewModel.TemplateList.Remove(model);

                        // 更新索引
                        UpdateTemplateIndices(viewModel);
                    }
                }
            }
        }

        private void Rename_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            if (button != null && button.DataContext is TemplateModel model)
            {
                var viewModel = DataContext as BeadGuideViewModel;
                EditRemarksView editView = EditRemarksView.Ins;
                if (model == null)
                    return;
                string modelName = model.Name;
                string name = editView.MessageBoxShow(modelName);
                if (editView.DialogResult == true)
                {
                    foreach (var item in viewModel.TemplateList)
                    {
                        if (item.Name == name)
                        {
                            MessageView.Ins.MessageBoxShow("名称重复！", eMsgType.Warn);
                            return;
                        }
                    }
                    model.Name = name;
                }
                if (viewModel != null)
                {

                    UpdateTemplateIndices(viewModel);
                }
            }

        }

        // Helper method to update indices
        private void UpdateTemplateIndices(BeadGuideViewModel viewModel)
        {
            for (int index = 0; index < viewModel.TemplateList.Count; index++)
            {
                viewModel.TemplateList[index].Index = index;
            }
        }
    }
}
